<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="作品ID" prop="videoId">
        <el-input
          v-model="queryParams.videoId"
          placeholder="请输入作品ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人昵称" prop="userName" label-width="100">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入发布人昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布人ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发布人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品标题" prop="videoTitle">
        <el-input
          v-model="queryParams.videoTitle"
          placeholder="请输入作品标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品分类" prop="videoClassify">
        <el-select v-model="queryParams.videoClassify" placeholder="请选择作品分类" clearable size="small" style="width:180px"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in videoClassifyTag" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布类型" prop="videoType">
        <el-select v-model="queryParams.videoType" placeholder="请选择发布类型" clearable size="small" style="width:180px"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in videoTypeTag" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定家乡" prop="hometownName">
        <el-input
          v-model="queryParams.hometownName"
          placeholder="请输入绑定家乡"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品状态" prop="videoStatus">
        <el-select v-model="queryParams.videoStatus" placeholder="请选择作品状态" clearable size="small" style="width:180px"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in videoStatusTag" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleUpdateAllStatus(1)"
          v-hasPermi="['store:video:edit']"
        >批量上架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleUpdateAllStatus(0)"
          v-hasPermi="['store:video:edit']"
        >批量下架</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange" :max-height="tableHeight()">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="作品ID" align="center" prop="videoId" width="180"/>
      <el-table-column label="用户昵称" align="center" prop="userName" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="作品标题" align="center" prop="videoTitle" width="150"/>
      <el-table-column label="作品分类" align="center" prop="videoClassify">
        <template v-slot="scope">
          <dic-staus-tag :options="videoClassifyTag" :staus="scope.row.videoClassify"></dic-staus-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布类型" align="center" prop="videoType">
        <template v-slot="scope">
          <dic-staus-tag :options="videoTypeTag" :staus="scope.row.videoType"></dic-staus-tag>
        </template>
      </el-table-column>
      <el-table-column label="绑定家乡" align="center" prop="hometownAddress" />
      <el-table-column label="发布时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="作品状态" align="center" prop="videoStatus">
        <template v-slot="scope">
          <dic-staus-tag :options="videoStatusTag" :staus="scope.row.videoStatus"></dic-staus-tag>
        </template>
      </el-table-column>
      <el-table-column label="评论数" align="left" prop="videoReviewNum" width="80">
        <template slot-scope="scope"><span class="loss-num">{{ scope.row.videoReviewNum }}</span></template>
      </el-table-column>
      <el-table-column label="点赞数" align="left" prop="videoUpvoteNum" width="80">
        <template slot-scope="scope"><span class="info-num">{{ scope.row.videoUpvoteNum }}</span></template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="videoRemark" />
      <el-table-column label="操作" align="center" fixed="right" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['store:video:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateStatus(scope.row,1)"
            v-hasPermi="['store:video:edit']"
          >上架</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateStatus(scope.row,0)"
            v-hasPermi="['store:video:edit']"
          >下架</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:video:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改家乡印象-作品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body destroy-on-close>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户昵称 : " prop="userName">
          <span>{{ form.userName }}</span>
        </el-form-item>
        <el-form-item label="作品标题 : " prop="videoTitle">
          <span>{{ form.videoTitle }}</span>
        </el-form-item>
        <el-form-item label="作品内容 : " prop="videoContent">
          <span>{{ form.videoContent }}</span>
        </el-form-item>
        <el-form-item label="作品视频 : " prop="videoResource" v-if="form.videoClassify === 0">
          <video width="500" v-if="videoDialog"  :id="'video'+form.videoId" height="270" class="tc-video-container" playsinline webkit-playinline x5-playinline></video>
        </el-form-item>
        <el-form-item label="作品图片 : " prop="videoResource" v-if="form.videoClassify === 1">
          <image-preview v-for="imgUrl in (form.pictureUrls.split(','))" :key="imgUrl" :src="imgUrl" :width="200" :height="200" />
        </el-form-item>

        <el-form-item label="绑定家乡 : " prop="hometownAddress">
          <span>{{ form.hometownAddress }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改特色游玩对话框 -->
    <el-dialog :title="titleStatus" :visible.sync="openStatus" width="500px" append-to-body>
      <el-form ref="form" :model="formStatus" :rules="rulesStatus" label-width="80px">
        <el-form-item label="下架原因" prop="videoRemark">
          <el-input v-model="formStatus.videoRemark" placeholder="请输入下架原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelStatus">取 消</el-button>
      </div>
    </el-dialog>
<!--    <el-button  type="text" @click="lookExamVideo()" >查看考试录像</el-button>-->
<!--    <el-dialog title="查看考试录像"-->
<!--               :close-on-click-modal="false"-->
<!--               :close-on-press-escape="false"-->
<!--               :visible.sync="videoDialog"-->
<!--               width="550px"  style="text-align:center;margin-top:70px;"-->
<!--    >-->
<!--      <video width="500" v-if="videoDialog"  :id="tcPlayerId" height="270" class="tc-video-container" playsinline webkit-playinline x5-playinline></video>-->
<!--    </el-dialog>-->
  </div>
</template>
<script>
  import {
    addVideo,
    delVideo,
    getVideo,
    listVideo,
    editStatus,
    updateVideo
  } from "@/api/store/video";
  import TCPlayer from "@/plugins/release/tcplayer.v5.1.0.min.js";

  export default {
  name: "Video",
  data() {
    return {
      videoClassifyTag: [
        { value: 0, label: "视频", cssClass: "success" },
        { value: 1, label: "图片", cssClass: "danger" }
      ],
      videoTypeTag: [
        { value: '0', label: "乡音乡貌", cssClass: "success" },
        { value: '1', label: "家乡美食", cssClass: "danger" },
        { value: '2', label: "周边景点", cssClass: "info" },
        { value: '3', label: "新闻资讯", cssClass: "warning" }
      ],
      videoStatusTag: [
        { value: 0, label: "下架", cssClass: "warning" },
        { value: 1, label: "上架", cssClass: "success" }
      ],
      tcPlayerId: 'tcPlayer',
      player: null,
      videoFileid: '',
      videoAppid: '',
      videoDialog:false,//查看录像
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家乡印象-作品表格数据
      videoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      titleStatus: "",
      // 是否显示弹出层
      openStatus: false,

      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        hometownId: undefined,
        videoTitle: undefined,
        videoResource: undefined,
        videoType: undefined,
        videoClassify: undefined,
        videoStatus: undefined,
        videoUpvoteNum: undefined,
        videoReviewNum: undefined,
        videoHeat: undefined,
        videoRemark: undefined,
      },
      // 表单参数
      form: {},
      formStatus: {},
      // 表单校验
      rules: {
      },
      rulesStatus: {
        playRemark: [
          {required: true, trigger: "blur", message: "请输入下架原因"}
        ],
      }

    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // const video = document.getElementById('player-container-id');
    // var player = TCPlayer(video, { // player-container-id 为播放器容器ID，必须与html中一致
    //   fileID: "1397757895540445539", // 请传入需要播放的视频fileID 必须
    //   appID: "1330025456", // 请传入点播账号的子应用appID 必须
    //   psign: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6MTMzMDAyNTQ1NiwiZmlsZUlkIjoiMTM5Nzc1Nzg5NTU0MDQ0NTUzOSIsImN1cnJlbnRUaW1lU3RhbXAiOjE3MjkzMDUxMDUsImNvbnRlbnRJbmZvIjp7ImF1ZGlvVmlkZW9UeXBlIjoiT3JpZ2luYWwiLCJpbWFnZVNwcml0ZURlZmluaXRpb24iOjEwfSwidXJsQWNjZXNzSW5mbyI6eyJkb21haW4iOiIxMzMwMDI1NDU2LnZvZC1xY2xvdWQuY29tIiwic2NoZW1lIjoiSFRUUFMifX0.aQ0Vk3vaR_sdb5B1qlBdXx1OZaAzCev1UoE33kFImWM", // 请传入播放器签名psign 必须
    //   licenseUrl: 'https://license.vod2.myqcloud.com/license/v2/1330025456_1/v_cube.license',
    //   autoplay: true // 是否自动播放
    //   // 其他参数请在开发文档中查看
    // });
  },
  methods: {
    /** 查询家乡印象-作品列表 */
    getList() {
      this.loading = true;
      listVideo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        console.log(response)
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    tableHeight() {
      return document.body.clientHeight - 250;
    },
    //---------------------------------初始化腾讯云播放器------------------------------
    getVideoLang (fileID, videoSign,hometownId) {
      console.log(fileID);
      const playerParam = {
        fileID: fileID,
        appID: "1330025456", // 请传入点播账号的子应用appID 必须
        psign: videoSign, // 请传入播放器签名psign 必须
        licenseUrl: 'https://license.vod2.myqcloud.com/license/v2/1330025456_1/v_cube.license',
        autoplay: false // 是否自动播放
      }
      console.log(this.tcPlayerId)
      this.player = TCPlayer(this.tcPlayerId, playerParam)
    },
    addDateRange(params, dateRange) {
      let search = params;
      dateRange = Array.isArray(dateRange) ? dateRange : [];
      search['startDate'] = dateRange[0];
      search['endDate'] = dateRange[1];
      return search;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelStatus() {
      this.openStatus = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: undefined,
        userId: undefined,
        hometownId: undefined,
        videoTitle: undefined,
        videoResource: undefined,
        videoType: undefined,
        videoClassify: undefined,
        videoStatus: 0,
        videoUpvoteNum: undefined,
        videoReviewNum: undefined,
        videoHeat: undefined,
        videoRemark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
      this.formStatus = {
        videoId: undefined,
        type: undefined,
        videoStatus: undefined,
        status: undefined,
        videoRemark: undefined
      };
      this.resetForm("formStatus");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.videoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家乡印象-作品";
    },
    /** 修改按钮操作 */
    handleView(row) {
      this.loading = true;
      this.reset();
      const videoId = row.videoId
      getVideo(videoId).then(response => {
        console.log(response)
        this.form = response.data;
        this.loading = false;
        this.open = true;
        this.title = "查看作品详情";
        this.videoDialog=true
        let self = this
        let fileId = response.data.videoResource
        let videoSign = response.data.videoSign
        let hometownId = response.data.hometownId;
        let videoClassify = response.data.videoClassify;
        if(videoClassify === 0){
          this.tcPlayerId = 'video'+response.data.videoId;
          console.log(this.tcPlayerId)
          if(fileId !== null && fileId !== '' && fileId !== undefined){
            setTimeout(() => {
              // self.videoFileid = fileId
              // self.videoSign = videoSign
              // self.hometownId = hometownId
              self.$nextTick(() => {
                self.getVideoLang(fileId, videoSign,hometownId )
              })
            },100)
          }
        }
      });
    },
    // handleUpdateStatus(row,videoStatus) {
    //   this.reset();
    //   const videoId = row.videoId
    //   let content = '是否确定上架作品？上架后作品将公开展示。';
    //   if(videoStatus === 0){
    //     content = '是否确定下架作品？下架后作品将不再公开展示。';
    //   }
    //   this.buttonLoading = true;
    //   this.$modal.confirm(content).then(() => {
    //     this.buttonLoading = true;
    //     this.form.videoId = videoId
    //     this.form.videoStatus = videoStatus
    //     return updateVideo(this.form)
    //   }).then(() => {
    //     this.buttonLoading = false;
    //     this.getList();
    //     if(videoStatus === 0){
    //       this.$modal.msgSuccess("下架成功");
    //     }else{
    //       this.$modal.msgSuccess("上架成功");
    //     }
    //   }).finally(() => {
    //     this.buttonLoading = false;
    //   });
    // },
    handleUpdateStatus(row, videoStatus) {
      this.reset();
      const videoId = row.videoId
      let content = '是否确定上架作品？';
      if (videoStatus === 0) {
        this.reset();
        this.openStatus = true;
        this.titleStatus = "是否确定下架作品？";
        this.formStatus.type = 0;
        this.formStatus.videoId = videoId;
        this.formStatus.videoStatus = videoStatus
      } else {
        this.buttonLoading = true;
        this.$modal.confirm(content).then(() => {
          this.buttonLoading = true;
          this.form.videoId = videoId
          this.form.videoStatus = videoStatus
          return updateVideo(this.form)
        }).then(() => {
          this.buttonLoading = false;
          this.getList();
          if(videoStatus === 0){
            this.$modal.msgSuccess("下架成功");
          }else{
            this.$modal.msgSuccess("上架成功");
          }
        }).finally(() => {
          this.buttonLoading = false;
        });
      }
    },
    // handleUpdateAllStatus(status) {
    //   let content = '是否确定上架作品？上架后作品将公开展示。';
    //   if(status === 0){
    //     content = '是否确定下架作品？下架后作品将不再公开展示。';
    //   }
    //   const videoIds = this.ids;
    //   this.$modal.confirm(content).then(() => {
    //     this.loading = true;
    //     let videoData = {
    //       ids: videoIds,
    //       status: status
    //     };
    //     return editStatus(videoData);
    //   }).then(() => {
    //     this.loading = false;
    //     this.getList();
    //     if(status === 0){
    //       this.$modal.msgSuccess("下架成功");
    //     }else{
    //       this.$modal.msgSuccess("上架成功");
    //     }
    //   }).finally(() => {
    //     this.loading = false;
    //   });
    // },
    handleUpdateAllStatus(status) {
      this.reset();
      let content = '是否确定上架作品？';
      if (status === 0) {
        this.openStatus = true;
        this.titleStatus = "是否确定下架作品？";
        this.formStatus.type = 1;
        this.formStatus.status = status
      } else {
        const videoIds = this.ids;
        this.$modal.confirm(content).then(() => {
          this.loading = true;
          let videoData = {
            ids: videoIds,
            status: status
          };
          return editStatus(videoData);
        }).then(() => {
          this.loading = false;
          this.getList();
          if(status === 0){
            this.$modal.msgSuccess("下架成功");
          }else{
            this.$modal.msgSuccess("上架成功");
          }
        }).finally(() => {
          this.loading = false;
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const videoId = row.videoId
      this.$modal.confirm('是否确定上架作品？上架后作品将公开展示。').then(() => {
        this.loading = true;
        return delVideo(videoId);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if(this.formStatus.type === 0){
            updateVideo(this.formStatus).then(response => {
              this.$modal.msgSuccess("下架成功");
              this.openStatus = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else if(this.formStatus.type === 1){
            const videoIds = this.ids;
            let videoData = {
              ids: videoIds,
              status: this.formStatus.status,
              remark: this.formStatus.videoRemark
            };
            editStatus(videoData).then(response => {
              this.$modal.msgSuccess("下架成功");
              this.openStatus = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoIds = row.videoId || this.ids;
      this.$modal.confirm('是否确定删除作品？删除后作品信息无法恢复，包括内容、评论和点赞。').then(() => {
        this.loading = true;
        return delVideo(videoIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/video/export', {
        ...this.queryParams
      }, `video_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.over-num {
  color: #F56C6C;
  font-weight: bold;
}

.loss-num {
  color: #67C23A;
  font-weight: bold;
}

.info-num {
  color: #606266;
  font-weight: bold;
}
</style>
